<?php
	require_once("include/jara_fns.php");
	if(isset($_POST["login_action"])) {
		try {
			session_start();
			$username = $_POST["username"];
			$password = $_POST["password"];
			if(!get_magic_quotes_gpc()) {
				$username = addslashes($username);
				$password = addslashes($password);
			}
			$result = jara_user_authenticate($_POST["username"], $_POST["password"]);
			if($result == false) {
				jara_page_start("Login failure");
				echo "<p>\n";
				echo "\tYou have entered an invalid username or password.<br />\n";
				echo "\tPlease go back and try again.\n";
				echo "</p>";
				jara_page_end();
				exit;
			}
			else {
				header("Location: index.php");
			}
		}
		catch(JaraDatabaseException $ex) {
			jara_page_start("Login failure");
?>
	<p>
		A database error occured during the process of creating this page.
	</p>
	<p>
		Error type: <?php echo (($ex->getSqlStage() == 1) ? "MySQL connection error" : "MySQL query error"); ?><br />
		Error message: &quot;<?php echo $ex->getSqlMsg(); ?>&quot; [code <?php echo $ex->getSqlCode(); ?>]
	</p>
<?php
			jara_page_end();
		}
		catch(JaraGeneralException $ex) {
			jara_page_start("Login failure");
?>
	<p>
		A general error occured during the process of creating this page.
	</p>
	<p>
		Error message: &quot;<?php echo $ex->getMessage(); ?>&quot;
	</p>
<?php
			jara_page_end();
		}
		exit;
	}
	jara_page_start("Login");
?>
<p>
	You must log in to access the administration interface.
</p>
<form action="login.php" method="post">
	<input type="hidden" name="login_action" id="login_action" value="login" />
	<table border="0">
		<tr class="login-top">
			<td colspan="2">
				<strong>Login</strong>
			</td>
		</tr>
		<tr>
			<td>
				Username
			</td>
			<td>
				<input type="text" id="username" name="username" width="20" />
			</td>
		</tr>
		<tr>
			<td>
				Password
			</td>
			<td>
				<input type="password" id="password" name="password" width="20" />
			</tr>
		</tr>
		<tr>
			<td></td>
			<td>
				<input type="submit" value="Login" />
			</td>
		</tr>
	</table>
</form>
<?php
	jara_page_end();
	exit;
?>